using System;
using System.IO;
using System.Threading.Tasks;
using DailyPoetryX.Services;
using DailyPoetryX.Services.Implementations;
using Moq;

namespace DailyPoetryX.UnitTest.Helpers {
    /// <summary>
    /// 诗词存储帮助类
    /// </summary>
    public static class PoetryStorageHelper {
        /// <summary>
        /// 删除数据库文件。
        /// </summary>
        public static void RemoveDatabaseFile() {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            File.Delete(Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder
                    .LocalApplicationData), PoetryStorage.DbName));
        }

        /// <summary>
        /// 获得已初始化的诗词存储。
        /// </summary>
        public static async Task<PoetryStorage> GetInitializedPoetryStorage() {
            var preferenceStorageMock = new Mock<IPreferenceStorage>();
            preferenceStorageMock.Setup(p =>
                p.Get(PoetryStorageConstants.VersionKey, -1)).Returns(-1);
            var mockPreferenceStorage = preferenceStorageMock.Object;
            var _poetryStorage = new PoetryStorage(mockPreferenceStorage);
            await _poetryStorage.InitializeAsync();
            return _poetryStorage;
        }
    }
}